<template>
  <div id="body">
    <div id="background"></div>
    <div id="Inputbox">
      <div class="title">
        <p>
          <span v-if="$route.name==='register'">欢迎注册</span>
          <span v-if="$route.name==='login'">欢迎登录</span>
          <span><router-link to="/">去首页</router-link></span>
        </p>
        <p v-if="$route.name==='register'">已有账号？
          <router-link to="/account/login">立即登录</router-link>
        </p>
        <p v-if="$route.name==='login'">没有账号？
          <router-link to="/account/register">立即注册</router-link>
        </p>
      </div>
      <router-view></router-view>
      <div class="bottom">
        <button @click="sendUser" type="submit">提交</button>
      </div>
    </div>
  </div>
</template>

<script>

export default {
  name: "Account",
  data() {
    return {}
  },
  methods: {
    // 用于提交登录或者组测表单数据
    sendUser() {
      if (this.$route.name === 'login') {
        this.$store.dispatch('Account/login', 'login').then(value => {
          if (value.code === 200) {
            this.$message({
              message: value.error,
              type: 'success'
            })
            this.$cookies.set('userinfo', JSON.stringify(value.userinfo), 60 * 60 * 24 * 7)
            setTimeout(() => {
              this.$router.push({
                name: 'home'
              })
            }, 500)
          } else {
            this.$message({
              message: value.error,
              type: 'error'
            })
          }
        })
      } else if (this.$route.name === 'register') {
        this.$store.dispatch('Account/register', 'register').then(value => {
          if (value.code === 200) {
            this.$message({
              message: value.error,
              type: 'success'
            })
            this.$cookies.set('userinfo', JSON.stringify(value.userinfo), 60 * 60 * 24 * 7)
            setTimeout(() => {
              this.$router.push({
                name: 'home'
              })
            }, 500)
          } else {
            this.$message({
              message: value.error,
              type: 'error'
            })
          }
        })
      }
    },
  },
  // 组件内离开守卫
  beforeRouteLeave(to, from, next) {
    this.$store.commit('Account/cleadData', ['cleadData'])
    next()
  }
}
</script>

<style scoped lang="less">
#body {
  width: 100%;
  height: 100%;

  #background {
    height: 100%;
    width: 100%;
    background-image: url("./image/login.jpg");
    background-repeat: no-repeat;
    background-size: 100% 100%;
    background-attachment: fixed;
  }

  #Inputbox {
    height: 550px;
    width: 450px;
    position: absolute;
    top: 80px;
    right: 200px;
    border-radius: 12px;
    overflow: hidden;
    background-color: #EEEFF2;
    z-index: 1;
    padding: 0 35px;

    .title {
      width: 100%;
      height: 15%;
      margin-top: 20px;

      p {
        width: 100%;


      }

      p:nth-child(1) {
        display: flex;
        align-items: center;
        margin-bottom: 5px;

        span:nth-child(1) {
          font-size: 28px;
          font-weight: bold;
          margin-right: 5px;
        }
      }

      //p {
      //  color: #7C7C7C;
      //
      //  a {
      //    color: #294FE4;
      //  }
      //}
    }

    .bottom {
      position: relative;
      margin-top: 20px;

      button {
        width: 380px;
        height: 40px;
        position: absolute;
        left: 50%;
        transform: translateX(-50%);
        border-radius: 12px;
        border: 1px;
        background-color: orange;
        color: white;
        font-size: 16px;
      }
    }
  }
}
</style>